package com.baymax.test.scene.internal

import android.util.Log
import android.view.View
import kotlin.properties.Delegates

/**
 * @method:
 * @description:
 * <p>
 * 支持多列，不支持跨列
 * </p>
 *
 * @param null
 * @return
 * @date: 2023/2/3 11:09
 * @author: xuyujian
 */
class FitLineWrapper(
    v: View, style: Style, private val styles: List<Style>
) : Abstract21ViewWrapper(v, style) {

    private val maxStyle = styles.maxBy { it.width }
    private val fitLine = styles.filter { it.top - style.top < 4 && it.bottom - style.bottom < 4 }
        .sortedBy { it.left }

    //l , r
//    private val partition = fitLine.minus(style)
//        .partition { (it.left + it.width) <= this.style.left + this.style.width }

    fun log(m: String) {
        Log.d("FitLineWrapper", m)
    }

    init {
        require(styles.isNotEmpty())
        log("fitLine $fitLine")
        render(fitLine)
        log("fitLine $fitLine")
    }


    override fun onChangeWidth(o: Int, n: Int) {
        log("onChangeWidth $o -> $n")
        val zoom = o < n
        style.renderWidth = n
        if (zoom) {
            //放大动画,max缩小
            maxStyle.renderWidth -= n - o
        } else {
            //缩小动画，max放大
            maxStyle.renderWidth += o - n
        }

        render(fitLine)
    }

    private fun render(styles: List<Style>) {
        styles.reduce {  left, right ->
            right.renderLeft = left.renderRight + (right.left - left.right)
            right
        }
    }

}